package com.cheboohui.iqs.spider.pingan.price.get;

import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.common.basic.TimeUtil;

/**
 * ClassName: PriceToole<br>
 * Description: TODO (平安报价参数修改方法).<br>
 * Reason: TODO ADD REASON(可选) <br>
 *
 * @author xielonghai
 * @date: 2016年8月25日 下午2:31:08
 * @version V1.0
 * @since JDK 1.8
 */
public class PramtenTwo {

	public static JSONObject changeParamTwo(JSONObject request, JSONObject response, double price) {
		JSONObject quotationList = request.getJSONArray("quotationList").getJSONObject(0);
		Set<String> it = response.keySet();
		for (String k : it) {
			quotationList.put(k, response.get(k));
		}

		if (price > 0) {
			JSONArray arr = response.getJSONObject("applyQueryResult").getJSONArray("vehiclePriceList");
			for (int i = 0; i < arr.size(); i++) {
				if (price == arr.getJSONObject(i).getDouble("purchasePrice")) {
					quotationList.put("selectCircAutoModelCode", arr.getJSONObject(0).getString("circAutoModelCode"));
					break;
				}
			}
		}

		if ("chooseDecisionTree".equals(response.getString("processType"))) {// 渠道系数选择<最低>
			JSONObject decisionTreeResult = quotationList.getJSONObject("c01CaculateResult")
					.getJSONObject("c01ResultDTO").getJSONObject("decisionTreeResult");
			JSONArray schemes = decisionTreeResult.getJSONArray("schemes");
			if (null != schemes && !schemes.isEmpty()) {
				JSONObject min = schemes.getJSONObject(0);
				double rate = min.getDoubleValue("channelRate");
				for (int l = 0; l < schemes.size(); l++) {
					if (rate > schemes.getJSONObject(l).getDoubleValue("channelRate")) {
						min = schemes.getJSONObject(l);
						rate = min.getDoubleValue("channelRate");
					}
				}
				min.put("hasChecked", true);
				min.put("isChecked", "1");

				quotationList.remove("applyPlans");
				quotationList.remove("c01IsApply");
				quotationList.remove("c51IsApply");
				quotationList.put("commercialDecisionTreeResult", json(decisionTreeResult));
				quotationList.remove("combineQuotationNo");
				quotationList.remove("confirmTime");
				quotationList.remove("displayNo");
				quotationList.remove("selectCircAutoModelCode");
				quotationList.put("trafficDecisionTreeResult", json(response.getJSONObject("c51CaculateResult")
						.getJSONObject("c51ResultDTO").getJSONObject("decisionTreeResult")));

			}
		}

		if ("renewalAndC51DisTax".equals(response.getString("processType"))) {
			JSONObject quotationLists = request.getJSONArray("quotationList").getJSONObject(0);
			quotationLists.getJSONObject("voucher").getJSONObject("c01BaseInfo").put("renewalTypeCode", "1");
			JSONObject voucher = quotationLists.getJSONObject("voucher");
			String c01 = getNo(response.getJSONArray("c01PolicyList"));
			voucher.put("c01BaseInfo", set(voucher.getJSONObject("c01BaseInfo")));
			if (!StringUtil.isEmpty(c01)) {
				voucher.getJSONObject("c01BaseInfo").put("renewalTypeCode", "1");
				voucher.getJSONObject("c01BaseInfo").put("lastPolicyNo", c01);
			}
			voucher.put("c51BaseInfo", set(voucher.getJSONObject("c51BaseInfo")));
			String c51 = getNo(response.getJSONArray("c51PolicyList"));
			if (!StringUtil.isEmpty(c51)) {
				voucher.getJSONObject("c51BaseInfo").put("renewalTypeCode", "1");
				voucher.getJSONObject("c51BaseInfo").put("lastPolicyNo", c51);
			}
			quotationLists.remove("combineQuotationNo");
			quotationLists.remove("displayNo");
			quotationLists.remove("selectCircAutoModelCode");
			quotationLists.remove("c01CircInfoDTO");
			quotationLists.remove("c01IsApply");
			quotationLists.remove("c51IsApply");
			quotationLists.remove("c01RateFactorPremCalcResult");
			quotationLists.remove("c51CircInfoDTO");
			quotationLists.getJSONObject("applyQueryResult").getJSONObject("circInfoDTO").put("thirdVehicleInfoDTO",
					set(quotationLists.getJSONObject("applyQueryResult").getJSONObject("circInfoDTO")
							.getJSONObject("thirdVehicleInfoDTO")));
			quotationLists.getJSONObject("applyQueryResult").put("coveragePremiumList",
					set(quotationLists.getJSONObject("applyQueryResult").getJSONArray("coveragePremiumList")));
			quotationLists.getJSONObject("applyQueryResult").put("vehiclePriceList",
					set(quotationLists.getJSONObject("applyQueryResult").getJSONArray("vehiclePriceList")));
			quotationLists.put("circAutoModelType", set(quotationLists.getJSONObject("circAutoModelType")));
			voucher.put("applicantInfo", set(voucher.getJSONObject("applicantInfo")));
			voucher.put("baseInfo", set(voucher.getJSONObject("baseInfo")));

			voucher.put("c01DisplayRateFactorList", set(voucher.getJSONArray("c01DisplayRateFactorList")));
			voucher.put("c01DutyList", set(voucher.getJSONArray("c01DutyList")));
			voucher.put("c01ExtendInfo", set(voucher.getJSONObject("c01ExtendInfo")));

			voucher.put("c51DisplayRateFactorList", set(voucher.getJSONArray("c51DisplayRateFactorList")));
			voucher.put("c51DutyList", set(voucher.getJSONArray("c51DutyList")));
			voucher.put("c51ExtendInfo", set(voucher.getJSONObject("c51ExtendInfo")));
			voucher.put("extendInfo", set(voucher.getJSONObject("extendInfo")));
			voucher.put("insurantInfo", set(voucher.getJSONObject("insurantInfo")));
			voucher.put("ownerDriver", set(voucher.getJSONObject("ownerDriver")));
			voucher.put("paymentInfo", set(voucher.getJSONObject("paymentInfo")));
			voucher.put("saleInfo", set(voucher.getJSONObject("saleInfo")));
			voucher.put("totalAmount", "0");
			voucher.put("totalPremium", "0");
			voucher.put("vehicleTarget", set(voucher.getJSONObject("vehicleTarget")));
			JSONObject vehicleTaxInfo = set(voucher.getJSONObject("vehicleTaxInfo"));
			vehicleTaxInfo.put("deduction", "");
			vehicleTaxInfo.put("deductionDueCode", "");
			vehicleTaxInfo.put("deductionDueProportion", "");
			vehicleTaxInfo.put("deductionDueType", "");
			vehicleTaxInfo.put("delinquentTaxDue", true);
			vehicleTaxInfo.put("isNeedAddTax", "01");
			vehicleTaxInfo.put("totalTaxMoney", "");
			voucher.put("vehicleTaxInfo", vehicleTaxInfo);
		}

		return request;
	}

	private static JSONObject json(JSONObject data) {
//		JSONObject obj = new JSONObject();
//		Set<String> it = data.keySet();
//		for (String key : it) {
//			obj.put(key, data.get(key));
//		}
//		return obj;
		return JSON.parseObject(data.toJSONString());
	}

	/**
	 * 平安报价请求参数自定义修改方式一
	 * 
	 * @param request
	 *            上步请求报价参数String
	 * @param response
	 *            上步请求报价返回String
	 * @param price
	 *            选定车辆价格
	 * @return
	 */
	public static JSONObject changeParamOne(JSONObject request, JSONObject response, double price) {
		try {
			JSONObject quotationList = request.getJSONArray("quotationList").getJSONObject(0);
			Set<String> it = response.keySet();
			for (String k : it) {
				quotationList.put(k, response.get(k));
			}
			// quotationList.remove("confirmTime");

			if (price > 0) {
				JSONArray arr = response.getJSONObject("applyQueryResult").getJSONArray("vehiclePriceList");
				for (int i = 0; i < arr.size(); i++) {
					if (price == arr.getJSONObject(i).getDouble("purchasePrice")) {
						quotationList.put("selectCircAutoModelCode",
								arr.getJSONObject(0).getString("circAutoModelCode"));
						break;
					}
				}
			}

			return request;
		} catch (Exception e) {
			return null;
		}
	}

	private static JSONObject set(JSONObject obj) {
		if (null == obj)
			return null;
		JSONObject res = new JSONObject();
		Set<String> th = obj.keySet();
		for (String k : th) {
			if ("0.0".equals(obj.getString(k)))
				res.put(k, "0");
			else if ("1.0".equals(obj.getString(k)))
				res.put(k, "1");
			else
				res.put(k, obj.getString(k));
		}
		return res;
	}

	private static JSONArray set(JSONArray arr) {
		if (null == arr || arr.isEmpty())
			return null;

		JSONArray ar = new JSONArray();

		for (int j = 0; j < arr.size(); j++) {
			ar.add(set(arr.getJSONObject(j)));
		}

		return ar;
	}

	private static String getNo(JSONArray arr) {
		if (null == arr || arr.isEmpty())
			return "";

		if (1 == arr.size())
			return arr.getJSONObject(0).getString("policyNo");
		else {
			long max = 0;
			String policyNo = "";
			for (int i = 0; i < arr.size(); i++) {
				long time = TimeUtil.timeIsLong(arr.getJSONObject(i).getString("insuranceEndTime"),
						TimeUtil.DEFAULT_TIME);
				if (max < time) {
					max = time;
					policyNo = arr.getJSONObject(i).getString("policyNo");
				}
			}

			return policyNo;
		}
	}
}
